//
//  WBTabBarButton.m
//  1229-Weibo.
//
//  Created by 千锋 on 15/12/29.
//  Copyright (c) 2015年 千锋. All rights reserved.
//

#import "WBTabBarButton.h"

@implementation WBTabBarButton


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.ratio = 0.6;
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        //self.titleLabel.backgroundColor = [UIColor blueColor];
    }
    return self;
}

-(void)setRatio:(CGFloat)ratio
{
    if (ratio < 0 || ratio > 1) {
        NSException * execp = [NSException exceptionWithName:@"异常提示" reason:@"只能设置在 0~ 1" userInfo:nil];
        @throw execp;
    }else{
        _ratio = ratio;
    }
    
}

/**生成按钮内部图片的位置(frame)*/
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
    CGFloat imgX = 0 ;
    CGFloat imgY = 0 ;
    CGFloat imgW = CGRectGetWidth(self.frame);
    //根据自身的ratio系数，得到图片的高度
    CGFloat imgH = CGRectGetHeight(self.frame) * _ratio;
    return CGRectMake(imgX, imgY, imgW, imgH);
    
}

/**生成按钮内部titleLabel的位置(frame)*/
-(CGRect)titleRectForContentRect:(CGRect)contentRect{
    CGFloat titleX = 0;
    CGFloat titleY = CGRectGetHeight(contentRect) * _ratio;
    CGFloat titleW = CGRectGetWidth(contentRect);
    CGFloat titleH = CGRectGetHeight(contentRect) * (1 -_ratio);
    
    return CGRectMake(titleX, titleY, titleW, titleH);
    
}

-(void)setTabBarItem:(UITabBarItem *)tabBarItem{
//    [self setTitleColor:tabBarItem.title forState:UIControlStateNormal];
    [self setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [self setTitle:tabBarItem.title forState:UIControlStateNormal];
    [self setImage:tabBarItem.image forState:UIControlStateNormal];
    [self setImage:tabBarItem.selectedImage forState:UIControlStateSelected];
}
@end
